package com.ruxyeah.netty.server;

import com.ruxyeah.netty.server.handler.NettyServerHandlerInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.net.InetSocketAddress;

@Component
public class NettyServer {
    private Logger logger = LoggerFactory.getLogger(getClass());


    @Value("${netty.port}")
    private Integer port;
    /**
     * Netty Client Channel
     */
    private Channel channel;
    /**
     * boss 线程组，用于服务端接受客户端的连接
     */
    private EventLoopGroup parentGroup = new NioEventLoopGroup();
    /**
     * worker 线程组，用于服务端接受客户端的数据读写
     */
    private EventLoopGroup childGroup = new NioEventLoopGroup();
    private final NettyServerHandlerInitializer nettyServerHandlerInitializer;

    @Autowired
    public NettyServer(NettyServerHandlerInitializer nettyServerHandlerInitializer) {
        this.nettyServerHandlerInitializer = nettyServerHandlerInitializer;
    }

    @PostConstruct
    private void start() {
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(parentGroup, childGroup)
                    .channel(NioServerSocketChannel.class)  // <2.2.2> 指定 Channel 为服务端 NioServerSocketChannel
                    .localAddress(new InetSocketAddress(port)) // <2.2.3> 设置 Netty Server 的端口
                    .option(ChannelOption.SO_BACKLOG, 1024) // <2.2.4> 服务端 accept 队列的大小
                    .childOption(ChannelOption.SO_KEEPALIVE, true) // <2.2.5> TCP Keepalive 机制，实现 TCP 层级的心跳保活功能
                    .childOption(ChannelOption.TCP_NODELAY, true) // <2.2.6> 允许较小的数据包的发送，降低延迟
                    .childHandler(nettyServerHandlerInitializer);
            ChannelFuture f = bootstrap.bind().sync();
            f.channel().closeFuture().sync();

            // <2> 绑定端口，并同步等待成功，即启动服务端
            ChannelFuture future = bootstrap.bind().sync();
            if (future.isSuccess()) {
                channel = future.channel();
                logger.info("[start][Netty Server 启动在 {} 端口]", port);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    /**
     * 关闭 Netty Server
     */
    @PreDestroy
    public void shutdown() {
        // 关闭 Netty Server
        if (channel != null) {
            channel.close();
        }
        // 优雅关闭两个 EventLoopGroup 对象
        parentGroup.shutdownGracefully();
        childGroup.shutdownGracefully();
    }
}
